﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TaobaoDistrSales.Common;
using TaobaoDistrSales.Model;

namespace TaobaoDistrSales
{
    public partial class MainDashBoardControl : UserControl
    {
        #region Ctor

        public MainDashBoardControl()
        {
            InitializeComponent(); 
        }

        #endregion

        #region Properties

        public TaobaoProcessor TaobaoProcessor
        {
            get
            {
                return taobaoProcessor;
            }
            set
            {
                taobaoProcessor = value;
                if (value != null)
                {
                    taobaoProcessor.UpdateStatus = (status) =>
                    {                        
                        if (CurrentActionLabel.InvokeRequired)
                            CurrentActionLabel.Invoke(new Action<string>(s => {
                                CurrentActionLabel.Text = s;
                            }), status);
                         
                    };
                    CurrentShopLabel.Text = taobaoProcessor.ShopName;
                    taobaoProcessor.ShopChanged += new Action<Shop>(taobaoProcessor_ShopChanged);
                }
            }
        }

        void taobaoProcessor_ShopChanged(Shop obj)
        {
            if (CurrentActionLabel.InvokeRequired)
            {
                CurrentActionLabel.Invoke(new Action<Shop>(s =>
                {
                    CurrentShopLabel.Text = s.Name;
                }), obj); 
            }
        }

        private TaobaoProcessor taobaoProcessor = null;

        #endregion

        #region Load

        private void MainDashBoardControl_Load(object sender, EventArgs e)
        {
            NoticeLabel.Text = "在执行的过程中...";
            try
            { 
                taobaodbEntities entities = ResourceManager.Current.LoadEntity();
                OptionsConfig config = entities.OptionsConfig.FirstOrDefault();
                OutputTextBox.Visible = config != null ? config.showlog : true;
            }
            catch { }
        }

        #endregion

        #region Button Events

        private void StartButton_Click(object sender, EventArgs e)
        {
            if (TaobaoProcessor.OutputWriter == null)
            {
                TaobaoProcessor.OutputWriter = new TextBoxWriter(OutputTextBox);
            }
            if ( (TaobaoProcessor.Status == ProcessStatus.None||TaobaoProcessor.Status== ProcessStatus.Stoped) && StartButton.Text =="开始")
            {
                TaobaoProcessor.Start();
                StartButton.Text = "暂停";
            }
            else if ((TaobaoProcessor.Status == ProcessStatus.Started || TaobaoProcessor.Status == ProcessStatus.Resumed) && StartButton.Text == "暂停")
            {
                TaobaoProcessor.Suspend();
                StartButton.Text = "继续";
            }
            else if (TaobaoProcessor.Status == ProcessStatus.Suspended && StartButton.Text == "继续")
            {
                TaobaoProcessor.Resume();
                StartButton.Text = "暂停";
            }

        }
        
        private void ClearButton_Click(object sender, EventArgs e)
        {            
            TaobaoProcessor.Clear();

        }

        private void StopButton_Click(object sender, EventArgs e)
        {
            TaobaoProcessor.Stop(); 
            this.ParentForm.Close();
        }

        #endregion


        #region Assist Methods
        
        public void UpdateStatus(string status)
        {
            CurrentActionLabel.Text = status;
        }
        
        #endregion

        #region Dispose
         

        #endregion
    }
}
